//
//  HZJAreadyDownViewController.m
//  HZ_Beautiful_Music
//
//  Created by dllo on 16/4/8.
//  Copyright © 2016年 ui. All rights reserved.
//

#import "HZJAreadyDownViewController.h"
#import "HZJListTableViewCell.h"
#import "CSLPlayer.h"
#import <AVKit/AVKit.h>
#import <AVFoundation/AVFoundation.h>

//static SystemSoundID sound_queue = 1;
@interface HZJAreadyDownViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) NSMutableArray *data;
@property (nonatomic, strong) NSMutableArray *nameArr;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSFileManager * fileManager;
@property (nonatomic, strong) NSArray * tempFileList;

@property (nonatomic, assign) NSInteger num;
@property (nonatomic, copy) NSString *path;

@property (nonatomic, strong) CSLPlayer *player;

@property(nonatomic,strong)AVAudioPlayer *audioplayer;

@end

#define FILEPATH [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]

@implementation HZJAreadyDownViewController



- (void)viewDidLoad {
    [super viewDidLoad];
    self.data = [NSMutableArray arrayWithCapacity:0];
    self.nameArr = [NSMutableArray arrayWithCapacity:0];
    self.navigationItem.title = @"已下载的歌曲";
    
    self.path = [NSString stringWithFormat:@"%@/music", FILEPATH];
    self.fileManager = [NSFileManager defaultManager];
    self.tempFileList = [[NSArray alloc] initWithArray:[_fileManager contentsOfDirectoryAtPath:_path error:nil]];
    
    for (int i = 0; i < _tempFileList.count; i++) {
        NSString *string = _tempFileList[i];
        if ([[string pathExtension] isEqualToString:@"mp3"]) {
            [_data addObject:_tempFileList[i]];
        }
    }
    
    for (int i = 0; i < _data.count; i++) {
        NSString *string = _data[i];
        NSString *str = [string substringWithRange:NSMakeRange(0, string.length - 4)];
        [self.nameArr addObject:str];
    }
    [self createTable];
    
}



#pragma mark - 创建tableView
- (void)createTable {
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height - 120) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    
    [_tableView registerClass:[HZJListTableViewCell class] forCellReuseIdentifier:@"HZJListTableViewCell"];
    [self.view addSubview:_tableView];
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 50;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _data.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    HZJListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HZJListTableViewCell"];
    cell.numLable.text = [NSString stringWithFormat:@"%ld", (long)indexPath.row];
    cell.titleLable.text = self.nameArr[indexPath.row];
    cell.heart.hidden = YES;
    return cell;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    self.num = indexPath.row;
    NSArray *array = @[[NSString stringWithFormat:@"%ld", self.num], self.data];
    NSNotificationCenter *centerArr = [NSNotificationCenter defaultCenter];
    
    [centerArr postNotificationName:@"ARRAY" object:array userInfo:nil];
    
    [_tableView deselectRowAtIndexPath:indexPath animated:YES];
}


- (void)action {
    
    UIImageView *imageV = [[UIImageView alloc] init];
    imageV.frame = CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height - 44);
    imageV.image = [UIImage imageNamed:@"11"];
    imageV.backgroundColor = [UIColor redColor];
    [self.view addSubview:imageV];

}

//#pragma mark - 播放本地音乐
//- (void)playMusic {
//    
//    NSString *string = [NSString stringWithFormat:@"%@/%@", _path, _data[_num]];
//    NSURL *url = [NSURL fileURLWithPath:string];
//    self.player = [[CSLPlayer alloc] initWithURL:url];
//    [self.player setVolume:0.7];   //设置音量大小
//    [self.player play];
//
//}


- (void)viewWillAppear:(BOOL)animated {
    
    self.player = [[CSLPlayer alloc] init];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
